{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7fc49447",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is a sample Python script.\n",
    "import os\n",
    "import re\n",
    "from datetime import timedelta\n",
    "import pandas as pd\n",
    "\n",
    "HOUR = 60 * 60 * 1000\n",
    "MINUTE = 60 * 1000\n",
    "\n",
    "# 找rtc最相关的wakeup alarm\n",
    "file_id = 1\n",
    "all_attri_num = pd.DataFrame()\n",
    "\n",
    "# 将时间字符串转换为毫秒\n",
    "def parse_time(time_str):\n",
    "    h_match = re.search(r'(?P<hour>\\d+)h\\d+', time_str)\n",
    "    m_match = re.search(r'(?P<minute>\\d+)m\\d+', time_str)\n",
    "    s_match = re.search(r'(?P<second>\\d+)s\\d+', time_str)\n",
    "    ms_match = re.search(r'(?P<millisecond>\\d+)ms', time_str)\n",
    "\n",
    "    h = int(h_match.group('hour')) if h_match else 0\n",
    "    m = int(m_match.group('minute')) if m_match else 0\n",
    "    s = int(s_match.group('second')) if s_match else 0\n",
    "    ms = int(ms_match.group('millisecond')) if ms_match else 0\n",
    "\n",
    "    total_ms = h * 60 * 60 * 1000 + m * 60 * 1000 + s * 1000 + ms\n",
    "    if total_ms == 0:\n",
    "        return None\n",
    "    else:\n",
    "        return total_ms\n",
    "\n",
    "def get_time(time_str):\n",
    "    h_match = re.search(r'(?P<hour>\\d+)h\\d+', time_str)\n",
    "    m_match = re.search(r'(?P<minute>\\d+)m\\d+', time_str)\n",
    "    s_match = re.search(r'(?P<second>\\d+)s\\d+', time_str)\n",
    "    ms_match = re.search(r'(?P<millisecond>\\d+)ms', time_str)\n",
    "\n",
    "    h = int(h_match.group('hour')) if h_match else 0\n",
    "    m = int(m_match.group('minute')) if m_match else 0\n",
    "    s = int(s_match.group('second')) if s_match else 0\n",
    "    ms = int(ms_match.group('millisecond')) if ms_match else 0\n",
    "\n",
    "\n",
    "    return timedelta(hours=int(h),minutes=int(m), seconds=int(s), milliseconds=int(ms))\n",
    "\n",
    "# 处理每一行数据\n",
    "def process_drain_line(line):\n",
    "    screen_off_time = None\n",
    "    screen_on_time = None\n",
    "    charge_on_screen = None\n",
    "\n",
    "    time = parse_time(line.split()[1])\n",
    "\n",
    "    charge_search = re.search(r'charge=(\\d+)', line)\n",
    "    if charge_search:\n",
    "        charge = int(charge_search.group(1))\n",
    "\n",
    "    if \"-screen\" in line and screen_off_time is None:\n",
    "        screen_off_time = time\n",
    "\n",
    "    if \"+screen\" in line:\n",
    "        screen_on_time = time\n",
    "        charge_on_screen = charge\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "def parse_log_line(line):\n",
    "    line = line.lstrip()\n",
    "    match = re.search(r\"(\\d+ms).*?(wake_reason=0:\\\"\\d+ qpnp_rtc_alarm\\\"|\\\"\\*walarm\\*:|\\\"\\*alarm\\*:).*\",\n",
    "                      line)\n",
    "    if match:\n",
    "        return (get_time(line), match.group(2))\n",
    "\n",
    "    return None\n",
    "\n",
    "def parse_log(path):\n",
    "    f = open(path, encoding=\"utf-8\")\n",
    "    fileContent = f.readlines()\n",
    "    f.close()\n",
    "    return fileContent\n",
    "\n",
    "def parse_timestamp(timestamp_str):\n",
    "    m, s, ms = re.match(r\"\\+(\\d+)m(\\d+)s(\\d+)ms\", timestamp_str).groups()\n",
    "    return timedelta(minutes=int(m), seconds=int(s), milliseconds=int(ms))\n",
    "\n",
    "def is_less_than_half_second(t1, t2):\n",
    "    return abs(t1 - t2) < timedelta(seconds=0.5)\n",
    "\n",
    "def count_alarm_walarm(line):\n",
    "    alarm = 0\n",
    "    walarm = 0\n",
    "    line = line.lstrip()\n",
    "    if re.search(r':\"\\*walarm\\*:',line):\n",
    "        walarm = 1\n",
    "    if re.search(r':\"\\*alarm\\*:',line):\n",
    "        alarm = 1\n",
    "    return walarm,alarm\n",
    "\n",
    "def get_rtc_alarm_num(line):\n",
    "    match = re.search(r'.*qpnp_rtc_alarm:.*(\\d+)\\s*times', line)\n",
    "    if match:\n",
    "        qpnp_rtc_alarm = int(match.group(1))\n",
    "    return qpnp_rtc_alarm\n",
    "\n",
    "\n",
    "def count_event(content):\n",
    "    batteryContentFlag = False\n",
    "    batteryContentOutFlag = False\n",
    "\n",
    "    wakeupContentFlag = False\n",
    "    wakeupContentOutFlag = False\n",
    "    # qpnp_rtc_alarm出现wakelock为walarm或者alarm次数\n",
    "    walarm_rtc_count = 0\n",
    "    alarm_rtc_count = 0\n",
    "\n",
    "    # wakelock为walarm或者alarm次数\n",
    "    w_count = 0\n",
    "    a_count = 0\n",
    "\n",
    "    atc_drain = 0\n",
    "    duration = \"0\"\n",
    "    qpnp_rtc_alarm = 0\n",
    "    app_wakeup_alarm_num = 0\n",
    "\n",
    "    prev_timestamp = None\n",
    "\n",
    "    # 计算真实耗电相关变量\n",
    "    time = None\n",
    "    screen_off_time = None\n",
    "    screen_on_time = None\n",
    "    charge_start = None\n",
    "    charge_end = None\n",
    "    charge_time = None\n",
    "    charge_start_flag = False\n",
    "\n",
    "    # time pattern\n",
    "\n",
    "    app_wakeup_num = 0\n",
    "    app_wakeup = pd.DataFrame()\n",
    "\n",
    "    for line in content:\n",
    "        # Judge arrive batterystats paragraph\n",
    "        line = line.lstrip()\n",
    "        if not batteryContentFlag:\n",
    "            if re.search(r\"DUMP OF SERVICE batterystats:.*\", line):\n",
    "                batteryContentFlag = True\n",
    "\n",
    "        # Judge out batterystats paragraph\n",
    "        if (batteryContentOutFlag is False) and (batteryContentFlag is True):\n",
    "            if re.search(r\"Per-PID Stats:.*\", line):\n",
    "                batteryContentOutFlag = True\n",
    "                batteryContentFlag = False\n",
    "\n",
    "        # Judge arrive app status\n",
    "        if (batteryContentOutFlag is True) and (wakeupContentFlag is False):\n",
    "            if re.search(\"Resource Power Manager Stats\", line):\n",
    "                wakeupContentFlag = True\n",
    "\n",
    "        # Judge out app status\n",
    "        if (wakeupContentOutFlag is False) and (wakeupContentFlag is True):\n",
    "            if re.search(\"Total cpu time reads: \", line):\n",
    "                wakeupContentFlag = False\n",
    "                wakeupContentOutFlag = True\n",
    "\n",
    "\n",
    "\n",
    "        if batteryContentFlag:\n",
    "            if line.split():\n",
    "                tmp = parse_time(line.split()[0])\n",
    "                if tmp is not None:\n",
    "                    time = tmp\n",
    "\n",
    "            # 计算真实耗电量\n",
    "            charge_search = re.search(r'charge=(\\d+)', line)\n",
    "            if charge_search:\n",
    "                charge = int(charge_search.group(1))\n",
    "                charge_time = time\n",
    "\n",
    "            # 当灭屏后，flag标志位置为true，记录第一个charge\n",
    "            if charge_start_flag:\n",
    "                charge_start_flag = False\n",
    "                charge_start = charge\n",
    "\n",
    "            if \"-screen\" in line and screen_off_time is None:\n",
    "                screen_off_time = time\n",
    "                charge_start_flag = True\n",
    "\n",
    "            if time is not None:\n",
    "                # 当打开屏幕时，统计之前第一个charge，并记录时间\n",
    "                if time > 3 * HOUR +59 * MINUTE:\n",
    "                    screen_on_time = parse_time(line.split()[0])\n",
    "                    charge_end = charge\n",
    "                    batteryContentFlag = False\n",
    "                    batteryContentOutFlag = True\n",
    "\n",
    "            # 统计qpnp_rtc_alarm\n",
    "            parsed_line = parse_log_line(line)\n",
    "\n",
    "\n",
    "            m,n=count_alarm_walarm(line)\n",
    "            w_count += m\n",
    "            a_count += n\n",
    "            if parsed_line is None:\n",
    "                # 匹配qpnp_rtc_alarm\n",
    "                rtc_match = re.search(r'qpnp_rtc_alarm:.*\\((\\d+) times\\)', line)\n",
    "                if rtc_match:\n",
    "                    qpnp_rtc_alarm = int(rtc_match.group(1))\n",
    "\n",
    "\n",
    "                # 匹配actual drain\n",
    "                # Match the actual drain numbers and calculate the average\n",
    "                drain_match = re.search(r'actual drain: (\\d+\\.?\\d*)-(\\d+\\.?\\d*)', line)\n",
    "                if drain_match:\n",
    "                    num1 = float(drain_match.group(1))\n",
    "                    num2 = float(drain_match.group(2))\n",
    "                    atc_drain = (num1 + num2) / 2\n",
    "\n",
    "                # Match the durations\n",
    "                duration_matches = re.search(r'idle:.*duration: ([\\dhms\\. ]+)', line)\n",
    "                if duration_matches:\n",
    "                    duration = duration_matches.group(1)\n",
    "                continue\n",
    "\n",
    "\n",
    "            timestamp, event = parsed_line\n",
    "\n",
    "            if 'wake_reason=0:\"546 qpnp_rtc_alarm\"' in event:\n",
    "                prev_timestamp = timestamp\n",
    "            elif prev_timestamp is not None and '*walarm*:' in event and abs(prev_timestamp - timestamp) < timedelta(\n",
    "                    seconds=0.5):\n",
    "                walarm_rtc_count += 1\n",
    "                prev_timestamp = None\n",
    "            elif prev_timestamp is not None and '*alarm*:' in event and abs(prev_timestamp - timestamp) < timedelta(\n",
    "                    seconds=0.5):\n",
    "                alarm_rtc_count += 1\n",
    "                prev_timestamp = None\n",
    "\n",
    "        if wakeupContentFlag:\n",
    "            # 统计App wakeup alarm\n",
    "            # if \"Wakeup alarm *walarm*:\" in line:\n",
    "\n",
    "            # 根据apk名称统计\n",
    "            app_name_pattern = re.search(r'Apk\\s(.+?):',line)\n",
    "            if app_name_pattern:\n",
    "                if app_wakeup_num != 0:\n",
    "                    app_wakeup[app_name] = [attri_num]\n",
    "                app_wakeup_num = 0\n",
    "                app_name = app_name_pattern.group(1)\n",
    "            s_app_wakeup = re.search(r'Wakeup alarm \\*walarm\\*:(.+?):\\s(\\d+)\\s+times', line)\n",
    "            if s_app_wakeup:\n",
    "                attri = s_app_wakeup.group((1))\n",
    "                attri_num = int(s_app_wakeup.group((2)))\n",
    "                app_wakeup_alarm_num += attri_num\n",
    "                app_wakeup_num += attri_num\n",
    "        if wakeupContentOutFlag :\n",
    "            if app_wakeup_num != 0:\n",
    "                app_wakeup[app_name] = [attri_num]\n",
    "            break\n",
    "\n",
    "\n",
    "            # TODO:统计不同应用的waekup alarm，找到与qpnp_rtc_alarm关系最大的那个\n",
    "\n",
    "    print(\"App wakeup alarm：\"+str(app_wakeup_alarm_num))\n",
    "\n",
    "    # 增加属性\n",
    "    app_wakeup[\"wakeup_alarm\"] = app_wakeup_alarm_num\n",
    "    app_wakeup[\"rtc_alarm\"] = walarm_rtc_count + alarm_rtc_count\n",
    "\n",
    "    # 将文件中的wakeup数量增加到全局变量中\n",
    "    global all_attri_num\n",
    "    all_attri_num = pd.concat([all_attri_num, app_wakeup], axis=0, ignore_index=True)\n",
    "\n",
    "    if charge_end == None:\n",
    "        charge_end = charge\n",
    "    if screen_on_time == None:\n",
    "        screen_on_time = charge_time\n",
    "\n",
    "    total_hour = (screen_on_time - screen_off_time) / (60 * 60 * 1000)\n",
    "    total_drain = charge_start - charge_end\n",
    "    avg_drain = total_drain / total_hour\n",
    "    return walarm_rtc_count + alarm_rtc_count, w_count + a_count,qpnp_rtc_alarm,atc_drain,duration,avg_drain,total_hour\n",
    "\n",
    "\n",
    "def count_Walarm_Qpnp(path):\n",
    "    fileContent = parse_log(path)\n",
    "    walarm_alarm_rtc_count,w_a_count,rtc_count,actual_drain,duration,avg_drain,test_time = count_event(fileContent)\n",
    "    print(\"test duration:\" + duration)\n",
    "    print(\"test time:\" + str(test_time)+\"h\")\n",
    "    # print(\"qpnp_rtc_alarm:\" + str(rtc_count))\n",
    "    # print(\"actual drain:\" + f\"{actual_drain:.2f}\")\n",
    "    print(\"walarm&rtc_alarm Count+alarm&rtc_alarm Count:\" + str(walarm_alarm_rtc_count) )\n",
    "    print(\"alarm Count + walarm Count:\" + str(w_a_count))\n",
    "    print(\"average drain:\" + f\"{avg_drain:.2f}\"+\"\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "44bb9784",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fullAlign\n",
      "device3_0704_1.txt: \n",
      "App wakeup alarm：307\n",
      "test duration:0\n",
      "test time:3.9876605555555558h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:26\n",
      "alarm Count + walarm Count:118\n",
      "average drain:30.59\n",
      "\n",
      "device1_0706_1.txt: \n",
      "App wakeup alarm：201\n",
      "test duration:0\n",
      "test time:3.984521388888889h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:19\n",
      "alarm Count + walarm Count:46\n",
      "average drain:31.87\n",
      "\n",
      "device2_0706_1.txt: \n",
      "App wakeup alarm：160\n",
      "test duration:0\n",
      "test time:3.9833997222222224h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:22\n",
      "alarm Count + walarm Count:63\n",
      "average drain:29.87\n",
      "\n",
      "device2_0704_1.txt: \n",
      "App wakeup alarm：193\n",
      "test duration:0\n",
      "test time:3.985679166666667h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:39\n",
      "alarm Count + walarm Count:84\n",
      "average drain:31.11\n",
      "\n",
      "device3_0706_1.txt: \n",
      "App wakeup alarm：286\n",
      "test duration:0\n",
      "test time:3.9863536111111113h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:30\n",
      "alarm Count + walarm Count:84\n",
      "average drain:27.34\n",
      "\n",
      "device1_0704_1.txt: \n",
      "App wakeup alarm：187\n",
      "test duration:0\n",
      "test time:3.9855502777777776h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:17\n",
      "alarm Count + walarm Count:34\n",
      "average drain:36.13\n",
      "\n",
      "align\n",
      "paper\n",
      "origin\n",
      "device1_0630_1.txt: \n",
      "App wakeup alarm：178\n",
      "test duration:0\n",
      "test time:3.9856694444444445h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:15\n",
      "alarm Count + walarm Count:35\n",
      "average drain:36.88\n",
      "\n",
      "device2_0630_1.txt: \n",
      "App wakeup alarm：216\n",
      "test duration:0\n",
      "test time:3.9838388888888887h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:28\n",
      "alarm Count + walarm Count:59\n",
      "average drain:34.39\n",
      "\n",
      "device1_0705_1.txt: \n",
      "App wakeup alarm：153\n",
      "test duration:0\n",
      "test time:3.984076111111111h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:13\n",
      "alarm Count + walarm Count:25\n",
      "average drain:33.63\n",
      "\n",
      "device2_0705_1.txt: \n",
      "App wakeup alarm：226\n",
      "test duration:0\n",
      "test time:3.9846427777777778h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:48\n",
      "alarm Count + walarm Count:66\n",
      "average drain:31.37\n",
      "\n",
      "device3_0630_1.txt: \n",
      "App wakeup alarm：321\n",
      "test duration:0\n",
      "test time:3.9845369444444443h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:28\n",
      "alarm Count + walarm Count:81\n",
      "average drain:28.86\n",
      "\n",
      "device3_0705_1.txt: \n",
      "App wakeup alarm：306\n",
      "test duration:0\n",
      "test time:3.986056111111111h\n",
      "walarm&rtc_alarm Count+alarm&rtc_alarm Count:29\n",
      "alarm Count + walarm Count:90\n",
      "average drain:26.84\n",
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 可选参数flight 、 wifi 、lte\n",
    "network_pattern = \"flight\"\n",
    "# fullAlign对应完整的预测模块，align对应部分预测模块，paper对应对比论文，origin对应原生\n",
    "strategies_list = [\"fullAlign\",\"align\",\"paper\",\"origin\"]\n",
    "data_path = \"batteryStats/oppo/v0.1\"\n",
    "\n",
    "# 将batterystats的txt文件放入下列路径中\n",
    "fullAlign_path = os.path.join(data_path, network_pattern, strategies_list[0])\n",
    "align_path = os.path.join(data_path, network_pattern, strategies_list[1])\n",
    "paper_path = os.path.join(data_path, network_pattern, strategies_list[2])\n",
    "origin_path = os.path.join(data_path, network_pattern, strategies_list[3])\n",
    "# align_path = \"batteryStats/align\"\n",
    "# origin_path = \"batteryStats/origin\"\n",
    "\n",
    "strtegies_path = [fullAlign_path,align_path,paper_path,origin_path]\n",
    "\n",
    "# 不同策略下的数据\n",
    "stratgies = {\n",
    "    \"fullAlign\":{\"rtc_alarm\":[],\"drain\":[]},\n",
    "    \"align\":{\"rtc_alarm\":[],\"drain\":[]},\n",
    "    \"paper\":{\"rtc_alarm\":[],\"drain\":[]},\n",
    "    \"origin\":{\"rtc_alarm\":[],\"drain\":[]}\n",
    "}\n",
    "\n",
    "for i in range(len(strategies_list)):\n",
    "    print(strategies_list[i])\n",
    "    for file_name in os.listdir(strtegies_path[i]):\n",
    "        print(file_name + \": \")\n",
    "        count_Walarm_Qpnp(os.path.join(strtegies_path[i], file_name))\n",
    "        # 添加数据\n",
    "        stratgies.\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3ed89554",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>android</th>\n",
       "      <th>com.heytap.market</th>\n",
       "      <th>com.xunmeng.pinduoduo</th>\n",
       "      <th>com.tmall.wireless</th>\n",
       "      <th>com.taobao.taobao</th>\n",
       "      <th>com.eg.android.AlipayGphone</th>\n",
       "      <th>com.tencent.mobileqq</th>\n",
       "      <th>com.tencent.mm</th>\n",
       "      <th>com.xingin.xhs</th>\n",
       "      <th>com.moji.mjweather</th>\n",
       "      <th>cn.gov.tax.its</th>\n",
       "      <th>wakeup_alarm</th>\n",
       "      <th>rtc_alarm</th>\n",
       "      <th>com.android.providers.calendar</th>\n",
       "      <th>com.sina.weibo</th>\n",
       "      <th>com.baidu.searchbox</th>\n",
       "      <th>com.alibaba.android.rimet</th>\n",
       "      <th>com.zhihu.android</th>\n",
       "      <th>me.ele</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>20.0</td>\n",
       "      <td>1</td>\n",
       "      <td>307</td>\n",
       "      <td>26</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>74</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>201</td>\n",
       "      <td>19</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>14</td>\n",
       "      <td>3</td>\n",
       "      <td>19</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>160</td>\n",
       "      <td>22</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>17</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>33</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>193</td>\n",
       "      <td>39</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>72</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>286</td>\n",
       "      <td>30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>17</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>22</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>9.0</td>\n",
       "      <td>2</td>\n",
       "      <td>187</td>\n",
       "      <td>17</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>26</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>9.0</td>\n",
       "      <td>1</td>\n",
       "      <td>178</td>\n",
       "      <td>15</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>216</td>\n",
       "      <td>28</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>35</td>\n",
       "      <td>10.0</td>\n",
       "      <td>1</td>\n",
       "      <td>153</td>\n",
       "      <td>13</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>8</td>\n",
       "      <td>20</td>\n",
       "      <td>16</td>\n",
       "      <td>35</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>226</td>\n",
       "      <td>48</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>12</td>\n",
       "      <td>48</td>\n",
       "      <td>18</td>\n",
       "      <td>59</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>321</td>\n",
       "      <td>28</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>14</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>51</td>\n",
       "      <td>17</td>\n",
       "      <td>95</td>\n",
       "      <td>9.0</td>\n",
       "      <td>1</td>\n",
       "      <td>306</td>\n",
       "      <td>29</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    android  com.heytap.market  com.xunmeng.pinduoduo  com.tmall.wireless   \n",
       "0         1                  1                     16                 1.0  \\\n",
       "1         0                  1                     11                 1.0   \n",
       "2         1                  1                     10                 NaN   \n",
       "3         1                  1                     17                 1.0   \n",
       "4         1                  1                     18                 1.0   \n",
       "5         1                  1                     17                 1.0   \n",
       "6         1                  3                     14                 1.0   \n",
       "7         1                  1                     14                 1.0   \n",
       "8         1                  1                     11                 NaN   \n",
       "9         1                  1                     14                 1.0   \n",
       "10        1                  2                     16                 1.0   \n",
       "11        1                  1                     14                 2.0   \n",
       "\n",
       "    com.taobao.taobao  com.eg.android.AlipayGphone  com.tencent.mobileqq   \n",
       "0                 1.0                            1                    19  \\\n",
       "1                 2.0                            0                     1   \n",
       "2                 1.0                           14                     3   \n",
       "3                 NaN                            1                    33   \n",
       "4                 1.0                            1                     1   \n",
       "5                 1.0                            2                    22   \n",
       "6                 1.0                            2                    26   \n",
       "7                 1.0                            1                     4   \n",
       "8                 1.0                            1                     2   \n",
       "9                 1.0                            8                    20   \n",
       "10                1.0                           12                    48   \n",
       "11                1.0                            1                    51   \n",
       "\n",
       "    com.tencent.mm  com.xingin.xhs  com.moji.mjweather  cn.gov.tax.its   \n",
       "0               17               2                20.0               1  \\\n",
       "1                7              74                 1.0               1   \n",
       "2               19               1                 NaN               1   \n",
       "3               17               1                 NaN               1   \n",
       "4               16              72                 1.0               1   \n",
       "5                3               3                 9.0               2   \n",
       "6                5               2                 9.0               1   \n",
       "7               17               1                 NaN               1   \n",
       "8                2              35                10.0               1   \n",
       "9               16              35                 NaN               1   \n",
       "10              18              59                 2.0               1   \n",
       "11              17              95                 9.0               1   \n",
       "\n",
       "    wakeup_alarm  rtc_alarm  com.android.providers.calendar  com.sina.weibo   \n",
       "0            307         26                             NaN             NaN  \\\n",
       "1            201         19                             1.0             1.0   \n",
       "2            160         22                             NaN             NaN   \n",
       "3            193         39                             NaN             1.0   \n",
       "4            286         30                             NaN             NaN   \n",
       "5            187         17                             1.0             1.0   \n",
       "6            178         15                             2.0             1.0   \n",
       "7            216         28                             NaN             NaN   \n",
       "8            153         13                             NaN             NaN   \n",
       "9            226         48                             NaN             NaN   \n",
       "10           321         28                             1.0             1.0   \n",
       "11           306         29                             NaN             NaN   \n",
       "\n",
       "    com.baidu.searchbox  com.alibaba.android.rimet  com.zhihu.android  me.ele  \n",
       "0                   NaN                        NaN                NaN     NaN  \n",
       "1                   NaN                        NaN                NaN     NaN  \n",
       "2                   NaN                        NaN                NaN     NaN  \n",
       "3                   1.0                        NaN                NaN     NaN  \n",
       "4                   NaN                        NaN                NaN     NaN  \n",
       "5                   NaN                        2.0               10.0     1.0  \n",
       "6                   NaN                        1.0                1.0     NaN  \n",
       "7                   NaN                        NaN                NaN     1.0  \n",
       "8                   NaN                        NaN                NaN     NaN  \n",
       "9                   NaN                        NaN                1.0     NaN  \n",
       "10                  NaN                        NaN                NaN     NaN  \n",
       "11                  NaN                        NaN                NaN     NaN  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分析隐藏属性和qpnp_rtc_alarm的关系\n",
    "all_attri_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "298631ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "corr_matrix = all_attri_num.corr()\n",
    "corr_qpnp_rtc_alarm = corr_matrix['rtc_alarm'].drop('rtc_alarm')\n",
    "top_5 = corr_qpnp_rtc_alarm.abs().sort_values(ascending=False).head(5).index.tolist()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a9ee11fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['com.alibaba.android.rimet',\n",
       " 'com.tencent.mm',\n",
       " 'com.android.providers.calendar',\n",
       " 'com.zhihu.android',\n",
       " 'com.xunmeng.pinduoduo']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a0bc4699",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['android', 'com.heytap.market', 'com.xunmeng.pinduoduo', 'com.tmall.wireless', 'com.taobao.taobao', 'com.eg.android.AlipayGphone', 'com.tencent.mobileqq', 'com.tencent.mm', 'com.xingin.xhs', 'com.moji.mjweather', 'cn.gov.tax.its', 'wakeup_alarm', 'rtc_alarm', 'com.android.providers.calendar', 'com.sina.weibo', 'com.baidu.searchbox', 'com.alibaba.android.rimet', 'com.zhihu.android', 'me.ele']\n"
     ]
    }
   ],
   "source": [
    "print(all_attri_num.columns.tolist())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9ba375cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "android                           0.223501\n",
       "com.heytap.market                -0.296912\n",
       "com.xunmeng.pinduoduo             0.370885\n",
       "com.tmall.wireless                0.038631\n",
       "com.taobao.taobao                -0.205032\n",
       "com.eg.android.AlipayGphone       0.196934\n",
       "com.tencent.mobileqq              0.282463\n",
       "com.tencent.mm                    0.692398\n",
       "com.xingin.xhs                    0.098990\n",
       "com.moji.mjweather               -0.149322\n",
       "cn.gov.tax.its                   -0.285873\n",
       "wakeup_alarm                      0.366925\n",
       "rtc_alarm                         1.000000\n",
       "com.android.providers.calendar   -0.551943\n",
       "com.sina.weibo                         NaN\n",
       "com.baidu.searchbox                    NaN\n",
       "com.alibaba.android.rimet         1.000000\n",
       "com.zhihu.android                -0.452463\n",
       "me.ele                                 NaN\n",
       "Name: rtc_alarm, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "corr_matrix['rtc_alarm']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5da8a45f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
